package com.producer.定向交换机;

import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class DirectQueueConfig {
    /**
     * 主队列交换机名称
     */
    public static final String DIRECT_EXCHANGE_NAME = "direct.exchange";
    /**
     * 然后创建死信队列交换机
     */
    @Bean("directExchange")
    public DirectExchange createExchange(@Qualifier("rabbitAdmin") RabbitAdmin rabbitAdmin){
        DirectExchange directExchange = new DirectExchange(DIRECT_EXCHANGE_NAME,false,false);
        rabbitAdmin.declareExchange(directExchange);
        return directExchange;
    }

    public static final String FIRST_DIRECT_QUEUE_ROUTINGKEY = "first.direct.queue.routingkey";
    public static final String FIRST_DIRECT_QUEUE_NAME = "firstDirectQueue";
    @Bean("firstDirectQueue")
    public Queue createFirstDirectQueue(@Qualifier("rabbitAdmin") RabbitAdmin rabbitAdmin,
                              @Qualifier("directExchange") DirectExchange exchange){
        Queue firstDirectQueue = new Queue(FIRST_DIRECT_QUEUE_NAME,false,false,false);
        rabbitAdmin.declareQueue(firstDirectQueue);
        rabbitAdmin.declareBinding(BindingBuilder.bind(firstDirectQueue).to(exchange).with(FIRST_DIRECT_QUEUE_ROUTINGKEY));
        return firstDirectQueue;
    }

    public static final String SECOND_DIRECT_QUEUE_ROUTINGKEY = "second.direct.queue.routingkey";
    public static final String SECOND_DIRECT_QUEUE_NAME = "secondDirectQueue";
    @Bean("secondDirectQueue")
    public Queue createSecondDirectQueue(@Qualifier("rabbitAdmin") RabbitAdmin rabbitAdmin,
                              @Qualifier("directExchange") DirectExchange exchange){
        Queue secondDirectQueue = new Queue(SECOND_DIRECT_QUEUE_NAME,false,false,false);
        rabbitAdmin.declareQueue(secondDirectQueue);
        rabbitAdmin.declareBinding(BindingBuilder.bind(secondDirectQueue).to(exchange).with(SECOND_DIRECT_QUEUE_ROUTINGKEY));
        return secondDirectQueue;
    }
}
